//
//  XZSelectCell.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/22.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "XZSelectCell.h"

@interface XZSelectCell()

@property (nonatomic, strong) UIImageView *iconImage;

@property (nonatomic, strong) UILabel *studentName;

@property (nonatomic, strong) UIButton *seltedBtn;


@end

@implementation XZSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setModel:(ZYstudentDetailModel *)model {
    _model = model;
    
    BOOL isUpPhoto = [NSString isBlankString:[model.owner valueForKey:@"avatar"]];
    if (!isUpPhoto) {
        [_iconImage sd_setImageWithURL:[NSURL URLWithString:[model.owner valueForKey:@"avatar"]] placeholderImage:[UIImage imageNamed:@"bg_picture_default"]];
    }
    
    BOOL isUp = [NSString isBlankString:[model.owner valueForKey:@"petName"]];
    if (isUp) {
        _studentName.text = @"";
    } else {
        _studentName.text = [model.owner valueForKey:@"petName"];
    }
    self.seltedBtn.selected = model.seleted;
}


- (void)setupUI {
    
    _iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_picture_default"]];
    
    _studentName = [[UILabel alloc] init];
    _studentName.font = [UIFont systemFontOfSize:14];
    _studentName.text = @"fasdfasdfsad";
    
    _seltedBtn = [[UIButton alloc] init];
    [_seltedBtn setImage:[UIImage imageNamed:@"icon_choice_default"] forState:(UIControlStateNormal)];
    [_seltedBtn setImage:[UIImage imageNamed:@"icon_choice_seleted"] forState:(UIControlStateSelected)];
    [_seltedBtn addTarget:self action:@selector(seletedStudent:) forControlEvents:UIControlEventTouchUpInside];
    _seltedBtn.layer.cornerRadius = _seltedBtn.width/2;
    _seltedBtn.layer.masksToBounds = YES;
    
    
    [self.contentView addSubview:_iconImage];
    [self.contentView addSubview:_studentName];
    [self.contentView addSubview:_seltedBtn];
    
    [_iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.offset(15);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [_studentName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_iconImage);
        make.left.equalTo(_iconImage.mas_right).offset(15);
    }];
    
    [_seltedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_iconImage);
        make.right.offset(-15);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}

#pragma mark - 多选
- (void)seletedStudent:(UIButton *)sender {
    
    if (sender.selected == YES) {
        sender.selected = NO;
        self.model.seleted = NO;
        
        if (_CallBack) {
            _CallBack();
        }
        
    } else {
        sender.selected = YES;
        self.model.seleted = YES;
        if (_CallBack) {
            _CallBack();
        }
    }
}

@end
